home *** CD-ROM | disk | FTP | other *** search
- /*
- * the class GRID
- * Copyright (C) 1997 Kazutaka Hirata <khirata@jove.acs.unt.edu>
- */
-
- #include "stdafx.h"
-
- #include "grid.h"
-
- GRID::GRID(uint zoom)
- {
- m_zoom = min(zoom, ZOOM_MAX);
- m_mat_size = double(m_zoom) / double(DIS_DINCH);
- m_pc_design_size.set(X_MAX * m_zoom / DIS_DINCH, Y_MAX * m_zoom / DIS_DINCH);
-
- m_ac_grid_origin .set(0, 0);
- m_ac_grid_origin_user.set(0, 0);
- m_main_grid_width = DIS_DINCH;
- m_sub_grid_width = DIS_DINCH;
-
- m_snap = TRUE;
- }
-
- void GRID::zoom_set(int zoom)
- {
- m_zoom = min(zoom, ZOOM_MAX);
- m_mat_size = double(m_zoom) / double(DIS_DINCH);
- m_pc_design_size.set(X_MAX * m_zoom / DIS_DINCH, Y_MAX * m_zoom / DIS_DINCH);
- }
-
- int GRID::zoom_get(void) const
- {
- return m_zoom;
- }
-
- const XY& GRID::get_pc_design_size(void) const
- {
- return m_pc_design_size;
- }
-
- void GRID::set_pc_win_size(const XY& pc_win_size)
- {
- m_pc_win_size = pc_win_size;
- }
-
- const XY& GRID::get_pc_win_size(void) const
- {
- return m_pc_win_size;
- }
-
- void GRID::snap_change(void)
- {
- m_snap = m_snap ? false : true;
- }
-
- void GRID::snap_set(uint snap)
- {
- m_snap = snap;
- }
-
- uint GRID::snap_get(void) const
- {
- return m_snap;
- }
-
- XYT GRID::distance_pc2ac(XYT pc) const
- {
- return XYT(double(pc) / m_mat_size);
- }
-
- XYT GRID::distance_ac2pc(XYT ac) const
- {
- return XYT(double(ac) * m_mat_size);
- }
-
- XY GRID::distance_ac2pc(const XY& ac) const
- {
- return ac * m_mat_size;
- }
-
- void GRID::snap(XY& ac) const
- {
- ac = ac - m_ac_grid_origin;
- ac = (ac + m_sub_grid_width / 2) / int(m_sub_grid_width) * m_sub_grid_width;
- ac = ac + m_ac_grid_origin;
- }
-
- void GRID::snap_min(XY& ac) const
- {
- ac = ac - m_ac_grid_origin;
- ac = ac / int(m_sub_grid_width) * m_sub_grid_width;
- ac = ac + m_ac_grid_origin;
- }
-
- void GRID::xy_pc2ac_core(const XY& pc, XY& ac) const
- {
- // XY pc_comp = m_pc_win_base + pc;
- XY pc_math;
- pc_math.set_x( pc.x());
- pc_math.set_y(m_pc_design_size.y() - pc.y());
- ac = pc_math / m_mat_size;
- }
-
- void GRID::xy_pc2ac_with_snap_on(const XY& pc, XY& ac) const
- {
- xy_pc2ac_core(pc, ac);
- snap(ac);
- }
-
- void GRID::xy_pc2ac_with_snap_off(const XY& pc, XY& ac) const
- {
- xy_pc2ac_core(pc, ac);
- }
-
- void GRID::xy_pc2ac(const XY& pc, XY& ac) const
- {
- if(m_snap) {
- xy_pc2ac_with_snap_on(pc, ac);
- } else {
- xy_pc2ac_with_snap_off(pc, ac);
- }
- }
-
- void GRID::xy_ac2pc(const XY& ac, XY& pc) const
- {
- XY pc_math = ac * m_mat_size;
- pc.set_x( pc_math.x());
- pc.set_y(m_pc_design_size.y() - pc_math.y());
- }
-
- void GRID::update_grid_origin(void)
- {
- int xr = m_ac_grid_origin_user.x() % m_main_grid_width;
- int yr = m_ac_grid_origin_user.y() % m_main_grid_width;
- xr -= m_main_grid_width;
- yr -= m_main_grid_width;
- m_ac_grid_origin = XY(xr, yr);
- }
-
- void GRID::set_grid_origin(const XY& ac_grid_origin_user)
- {
- m_ac_grid_origin_user = ac_grid_origin_user;
- update_grid_origin();
- }
-
- XY GRID::get_grid_origin(void) const
- {
- return m_ac_grid_origin;
- }
-
- void GRID::set_grid_width(uint grid_width, uint div)
- {
- m_main_grid_width = grid_width;
- m_sub_grid_width = grid_width / div;
- update_grid_origin();
- }
-
- uint GRID::get_main_grid_width(void) const
- {
- return m_main_grid_width;
- }
-
- uint GRID::get_sub_grid_width(void) const
- {
- return m_sub_grid_width;
- }
-
- void GRID::set_center(const XY& /*ac_center*/)
- {
- // XY pc_center = distance_ac2pc(ac_center);
- // pc_center.set_y(m_pc_design_size.y() - pc_center.y());
- // m_pc_win_base = pc_center - m_pc_win_size / 2;
- }
-
- void GRID::zoom_in(XY& pc_win_base)
- {
- uint zoom_old = m_zoom;
- uint zoom_new = min(uint(ZOOM_MAX), m_zoom * 3 / 2);
- if(zoom_old != zoom_new) {
- XY pc_center = pc_win_base + m_pc_win_size / 2;
- XY ac_center;
- xy_pc2ac_with_snap_off(pc_center, ac_center);
- zoom_set(zoom_new);
- xy_ac2pc(ac_center, pc_center);
- pc_win_base = pc_center - m_pc_win_size / 2;
- }
- }
-
- void GRID::zoom_out(XY& pc_win_base)
- {
- uint zoom_old = m_zoom;
- uint zoom_new = max(uint(ZOOM_MIN), m_zoom * 2 / 3);
- if(zoom_old != zoom_new) {
- XY pc_center = pc_win_base + m_pc_win_size / 2;
- XY ac_center;
- xy_pc2ac_with_snap_off(pc_center, ac_center);
- zoom_set(zoom_new);
- xy_ac2pc(ac_center, pc_center);
- pc_win_base = pc_center - m_pc_win_size / 2;
- }
- }
-
- void GRID::zoom_window(const XY& ac_min_src, const XY& ac_max_src, XY& pc_win_base)
- {
- XY ac_min = get_min(ac_min_src, ac_max_src);
- XY ac_max = get_max(ac_min_src, ac_max_src);
- if(ac_min != XY(X_MIN, Y_MIN) && ac_max != XY(X_MAX, Y_MAX)) {
- XY ac_size = ac_max - ac_min;
- if(m_pc_win_size.x() * ac_size.y() < m_pc_win_size.y() * ac_size.x()) {
- zoom_set(m_pc_win_size.x() * DIS_DINCH / ac_size.x());
- } else {
- zoom_set(m_pc_win_size.y() * DIS_DINCH / ac_size.y());
- }
- XY ac_center = (ac_min + ac_max) / 2;
- XY pc_center;
- xy_ac2pc(ac_center, pc_center);
- pc_win_base = pc_center - m_pc_win_size / 2;
- }
- }
-
- COLORREF GRID::get_grid_color (void) const { return RGB(255, 255, 255); }
- COLORREF GRID::get_cursor_color(void) const { return RGB(255, 255, 255); }
- COLORREF GRID::get_target_color(void) const { return RGB(255, 255, 255); }
- COLORREF GRID::get_erase_color (void) const { return RGB( 0, 0, 0); }
-
- COLORREF GRID::get_layer_color(int layer) const
- {
- COLORREF layer_color_table[] = {
- // RRR, GGG, BBB
- RGB(255, 0, 0),
- RGB( 0, 255, 0),
- RGB( 0, 0, 255),
- RGB(255, 255, 0),
- RGB( 0, 255, 255)
- };
- return layer_color_table[layer];
- }
-